<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');  

/**
 * Controller for Teams API.
 * @package backend-teams/conrollers
 * @category Controller
 * 
 * @author Mark Angelo Angulo
 * @copyright (c) 2015
 */
class Team_api extends REST_Controller
{
    /*
     * Constructor Method
     */
    public function __construct() {
        parent::__construct();
        
        $this->load->model('team_model');
        
        $this->load->helper('api');
        $this->load->helper('team');
        
        $this->load->library('form_validation');
    }
    
    public function index_get()
    {
        $params = get_params();

        $teams = $this->team_model->all_api($params['args'], $params['fields'], $params['limit'], $params['offset'], $params['sort'], $params['order'])->get();
        response_teams($teams);
    }
    
    public function index_post()
    {
        if( valid_add_team() )
        {
            $team = add_team();
            // if successful
            if( $team['id'] )
            {
                format_response('success', 201, 'Add Success', NULL, $team);
            } else
            {
                format_response('failed', 500, 'Server Error', array('database_error' => 'Database Error'));
            }
        } else
        {
            format_response('failed', 400, 'Invalid Parameter', $this->form_validation->error_array());
        }
    }

    
    public function index_put()
    {
        put_params();
        
        if( valid_edit_team() )
        {
            $team = edit_team();
            // if successful
            if( $team )
            {
                format_response('success', 200, 'Edit Success', NULL, $team);
            } else
            {
                format_response('failed', 500, 'Server Error', array('database_error' => 'Database Error'));
            }
        } else
        {
            format_response('failed', 400, 'Invalid Parameter', $this->form_validation->error_array());
        }
    }
    
    public function index_delete()
    {
        delete_params();
        
        if( valid_delete_team() )
        {
            $deleted = delete_team();
            if( $deleted === 'deleted' OR $deleted === 'archived' )
            {
                format_response('success', 200, 'Delete Success');
            } else
            {
                format_response('failed', 500, 'Server Error', array('database_error' => 'Database Error'));
            }
        } else
        {
            format_response('failed', 400, 'Invalid Parameter', $this->form_validation->error_array());
        }
    }

    public function team_by_id_get()
    {
        $params = get_params(4);

        $id = $this->uri->segment(3);
        
        $args = $params['args'];
        $args['id'] = $id;

        $teams = $this->team_model->all_api($args, $params['fields'], $params['limit'], $params['offset'], $params['sort'], $params['order'])->get();
        response_teams($teams);
    }
}

/* End of file teams.php */
/* Location: ./application/teams/backend-teams/controllers/teams.php */